//
//  LoginViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/29.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginMainView.h"
#import "RootTabBarController.h"
#import "LoginRequestTools.h"
#import "Mediator+Welcome.h"
#import "ASJFUserNavigationController.h"

@interface LoginViewController ()<LoginDelegate>

@property (nonatomic, strong) LoginMainView *loginMainView;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DuanXinYanZhengKeChongXinFaSong) name:@"DuanXinYanZhengKeChongXinFaSong" object:nil];
    [self initASJFUI];
    [[Mediator sharedInstance] HFQGuidePageChoiceAppear];
}

- (void)initASJFUI {
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.loginMainView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view.mas_left);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view.mas_top);
    }];
    
    if ([HFQCommonTools getVerificationCodeCountDown] < 60) {
        self.loginMainView.btnSelect = YES;
        self.loginMainView.codeTextView.codeButton.btnSelect = YES ;
    }
}
- (void)DuanXinYanZhengKeChongXinFaSong {
    self.loginMainView.btnSelect = NO;
}

//点击获取验证码的代理方法
- (void)getVerificationCodeWithDictionary:(NSDictionary *)dictionary {
    
    [HFQIndicationView showInView:self.view];
    [LoginRequestTools SendSmsWithPhoneNumber:dictionary[@"phone"] Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        
//        self.loginMainView.btnSelect = YES;
        self.loginMainView.codeTextView.codeButton.btnSelect = YES ;
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        self.loginMainView.btnSelect = NO;
        self.loginMainView.codeTextView.codeButton.btnSelect = NO ;
        
    }];
}

//点击登录的代理方法 前去登录
- (void)loginMainView:(NSDictionary *)dictionary {
    
    [HFQIndicationView showInView:self.view];
    [LoginRequestTools LoginWithParameters:dictionary Success:^(id  _Nullable responseObject) {
        [HFQIndicationView dismiss];
        NSDictionary *dataDic = responseObject;
        if (dataDic[@"data"][@"token"]) {
            [AD_UserDefaults setObject:[NSString stringWithFormat:@"%@",dataDic[@"data"][@"token"]] forKey:ASJFMyUserLoginTokenKey];
            [AD_UserDefaults setObject:[NSString stringWithFormat:@"%@",dataDic[@"data"][@"userName"]] forKey:ASJFUserNameKey];
            [AD_UserDefaults setObject:[NSString stringWithFormat:@"%@",dataDic[@"data"][@"phone"]] forKey:ASJFMyUserInfoAccountKey];
            [AD_UserDefaults synchronize];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateData_getAreaData" object:nil];
            
            [self cancelLogin];
        }else {
            [HUD_Mao showProgress:@"获取token失败，请重试"];
        }
        
    } Failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [HFQIndicationView dismiss];
        
    }];
}

-(void)cancelLogin {
    
    [self.view endEditing:YES];
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    if ([HFQCommonTools isEqualPhone]) {
        [userD setObject:@"1" forKey:@"IsShowLoginInfo_Mao"];
    }else {
        [userD setObject:@"0" forKey:@"IsShowLoginInfo_Mao"];
    }
    NSMutableDictionary *mutDic = [QuickDataLocalStorageTool QuickDataLocalStorageToolUserInfoRead];
    if (mutDic[@"UserName"]) {
        
        [JPUSHService setAlias:mutDic[@"UserName"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
    }
    
    
    UIWindow* window = UIApplication.sharedApplication.keyWindow;
    
    
    if ([window.rootViewController isMemberOfClass:[ASJFUserNavigationController class]]) {
        
        typedef void (^Animation)(void);
        
        Animation animation = ^{
            BOOL oldState = [UIView areAnimationsEnabled];
            [UIView setAnimationsEnabled:NO];
            window.rootViewController = [[RootTabBarController alloc] init];
            [UIView setAnimationsEnabled:oldState];
        };
        
        [UIView transitionWithView:window
                          duration:0.5f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:animation
                        completion:^(BOOL finished) {
                            
                        }];
    }else {
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    
    
    
    
    
    
    
}

- (LoginMainView *)loginMainView {
    
    if (!_loginMainView) {
        _loginMainView = [[LoginMainView alloc] init];
        _loginMainView.loginDelegate = self;
        [self.view addSubview:_loginMainView];
    }
    return _loginMainView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
